#include<iostream>
#include<cmath>
using namespace std;
const int N = 15;
int a[N], b[N];
int ret = 0x3f3f3f3f;
int s = 1, c = 0;
int n;
void dfs(int pos)
{
    if (pos > n) return ;
    // 选
    s *= a[pos];
    c += b[pos];
    ret = min(ret, abs(s - c));
    dfs(pos + 1);

    // 不选
    s /= a[pos];
    c -= b[pos];
    dfs(pos + 1);
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
    dfs(1);
    cout << ret << endl;
    return 0;
}